//
//  ZQADviewControlloer.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/5.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "ZQADviewControlloer.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "ZQADItem.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ZQTabBaController.h"



#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"



@interface ZQADviewControlloer ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UIView *ADView;
@property (nonatomic ,weak)UIImageView *imageView;
@property (nonatomic ,strong)ZQADItem *item;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
@property (nonatomic ,weak)NSTimer *timer;

@end
@implementation ZQADviewControlloer

/*
   完善了广告界面的业务逻辑
   点击广告图片跳转到网页
   广告界面倒计时
   点击跳过按钮跳转到主控制器
 
 */

- (UIImageView *)imageView
{
    if (_imageView == nil) {
        UIImageView *imageView = [[UIImageView alloc]init];
        _imageView = imageView;
        [self.ADView addSubview:imageView];
        
        //允许与用户进行交互
        imageView.userInteractionEnabled = YES;
        
        //创建点按手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        
        //将点按手势添加进view
        [imageView addGestureRecognizer:tap];
        
    }
    return _imageView;
}


//手势点按方法 :跳转到广告界面
- (void)tap
{
    
    UIApplication *app = [UIApplication sharedApplication] ;
    //判断网页是否可以打开
    if ([app canOpenURL:[NSURL URLWithString:_item.ori_curl]]) {
        //打开网页
        [app openURL:[NSURL URLWithString:_item.ori_curl]];
    }
    
}

//跳转按钮
- (IBAction)clickJump:(id)sender {
     //跳转主控制器
    ZQTabBaController *tabBar = [[ZQTabBaController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBar;
    
    //销毁定时器
    [_timer invalidate];
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    //加载广告启动页面
    [self setAD];
    
    //加载广告数据
    [self loadData];
    
    //加载定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    
}

- (void)timeChange
{
    //静态的, 只有一个
    static int i = 3;
    
    //判断3秒过后
    if (i <= 0) {
        //跳转主控制器
        [self clickJump:nil];
        
    };
    
    i--;
    //跳转按钮文字属性
    NSString *str = [NSString stringWithFormat:@"跳过(%d)",i];
    //设置跳转按钮的文字
    [_jumpBtn setTitle:str forState:UIControlStateNormal];
    
}

//加载广告数据
- (void)loadData
{
    //创建请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //创建请求参数字典
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"code2"] = code2;
    
    
    //content-type:数据格式
    //unacceptable content-type: text/html" 响应头
    //响应出问题 需要修改AFN框架内部
    //点击 manager -> self.responseSerializer = [AFJSONResponseSerializer serializer]响应体 点击serializer ->
    //   在 self.acceptableContentTypes  添加 text/html
    
    //发送请求任务
    [manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = [responseObject[@"ad"] firstObject];
        
        _item = [ZQADItem mj_objectWithKeyValues:dict];
        
        //防止除以0 会报错
        if (_item.w <= 0) return ;
        
        //展示界面
        CGFloat w = ZQScreenW;
        CGFloat h = ZQScreenW / _item.w * _item.h;
        
        self.imageView.frame = CGRectMake( 0, 0, w, h);
        
       
        
        //展示广告界面显示全屏
//        CGFloat x = 0;
//        if (h < ZQScreenH) {
//            h = ZQScreenH;
//            w = ZQScreenH / _item.h * _item.w;
//            if (w > ZQScreenW) {
//                x = (w - ZQScreenW) * 0.5;
//            }
//        }
//        self.imageView.frame = CGRectMake( -x, 0, w, h);

        //加载广告图片
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
        
        //请求成功, 写成plist文件`
        [responseObject writeToFile:@"/Users/Zhong/Desktop/我的代码/08 - 百思不得姐/ZQBaiSiBuDeJie/BuDeJie/BuDeJie/Classes/AD(广告)/ad.plist" atomically:YES];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
        
    }];
    
}




//加载启动图片
- (void)setAD
{
    UIImage *image = nil;
    
    //根据不同屏幕的高度 加载不同的图片
    if (iphone6P) {
        image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }else if (iphone6){
        image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    }else if (iphone5){
        image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    }else if (iphone4){
        image = [UIImage imageNamed:@"LaunchImage"];
    }
    
    self.imageV.image = image;
    
}
@end
